﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MockPrjTeam2.Biz;
using MockPrjTeam2.Entity;
namespace MockPrjTeam2.Pages.Forms
{
    public partial class AddOrganizations : System.Web.UI.Page
    {

        private static int[] _listOrgSv = new int[100];
        private static int[] _listOrgPr = new int[100];
        protected void Page_Load(object sender, EventArgs e)
        {

            if (Session["contact"] != null || Session["Address"] != null || Session["Bussname"] != null || Session["Sic"] != null||Session["service"]!=null||Session["program"]!=null )
            {
                
                var o = Session["contact"];
                if (o != null) txtContact.Text = o.ToString();
                var a = Session["Address"];
                if (a != null) txtPostCode.Text = a.ToString();
                var b = Session["Bussname"];
                if (b != null) txtBussinessName.Text = b.ToString();
                var s = Session["Sic"];
                if (s != null) TxtSicCode.Text = s.ToString();
              
            }

            if (!IsPostBack)
            {

                GetCountry();
                DetailsPanel03.Visible = false;
              

            }
         
            GetValueLookUp();
           

        }

        private void GetValueLookUp()
        {
            string script =
                @"<script>
              function OpenPopupBussiness() {
                popup('../Views/PopupSearchBussiness.aspx');
            }
                 function OpenPopup() {
                popup('PopupListContacts.aspx');
            }
              function OpenPopupAddress() {
                popup('../Views/PopupSearchAddress.aspx');
            }
            function popup(url) {

                var width = 500;
                var height = 400;
                var left = (screen.width - width) / 2;
                var top = (screen.height - height) / 2;
                var params = 'width=' + width + ', height=' + height;
                params += ', top=' + top + ', left=' + left;
                params += ', directories=no';
                params += ', location=no';
                params += ', menubar=no';
                params += ', resizable=no';
                params += ', scrollbars=no';
                params += ', status=no';
                params += ', toolbar=no';
                newwin = window.open(url, 'windowname5', params);
                if (window.focus) { newwin.focus() }
                return false;
            }
            </script>";
            if (!Page.ClientScript.IsStartupScriptRegistered("Mast"))
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(),
                                                        "Mast", script);
            }
        }


        private void GetService()
        {
            //cbl
            ServiceBiz serviceBiz = new ServiceBiz();
            CblServices.DataSource = serviceBiz.GetAll();
            CblServices.DataTextField = "ServiceName";
            CblServices.DataValueField = "ServiceID";
           
            CblServices.DataBind();
            
        }
        private void GetProgramme()
        {
            ProgrammeBiz programmeBiz = new ProgrammeBiz();
            CblPrograms.DataSource = programmeBiz.GetallProgramme();
            CblPrograms.DataTextField = "ProgrammeName";
            CblPrograms.DataValueField = "ProgrammeID";
           
            CblPrograms.DataBind();
        }
        private void GetCountry()
        {
            //ddl
            //OrganizationBus bus = new OrganizationBus();
            var bus = new CountryBiz();
            DdlNationCounty.DataSource = bus.GetAllCountry();
            DdlNationCounty.DataTextField = "CountryName";
            DdlNationCounty.DataValueField = "CountryID";
            DdlNationCounty.DataBind();

        }
        public int CheckOrganizationNameExist(string orgName)
        {
            OrganizationBuz organizationBuz=new OrganizationBuz();
            return organizationBuz.CheckOrganizationNameExist(orgName);
        }
        public void Success(string text)
        {
           
            LiteralSuccess.Text = text;
           
        }
        protected void ButtonSave_Click(object sender, EventArgs e)
        {
           
            OrganizationBuz bus = new OrganizationBuz();
            Organization organization = new Organization();
            int checkOrganizationName = CheckOrganizationNameExist(TxtOrganizationName.Text);
            if(checkOrganizationName>0)
            {
                lblCheckOrgName.Text = "OrganizationName does exist ! Please input new OrganizationName !";
               
                return;
            }
            organization.OrganizationName = TxtOrganizationName.Text;
            organization.ShortDescription = TxtShortDescription.Text;
            organization.ContactId = Convert.ToInt32(hdnContactId.Value);
            organization.ContactName = txtContact.Text;
            organization.AddressLine1 = TxtAddressLine1.Text;
            organization.AddressLine2 = TxtAddressLine2.Text;
            organization.AddressLine3 = TxtAddressLine3.Text;
            organization.AddressId = Convert.ToInt32(hdnAddressId.Value);
            organization.PostCode = txtPostCode.Text;
            organization.CityTown = TxtCityTown.Text;
            organization.County = TxtCounty.Text;
            organization.CountryId = Convert.ToInt32(DdlNationCounty.SelectedValue);
            organization.BusinessTypeId = Convert.ToInt32(hdnBussId.Value);
            organization.BussinessType = txtBussinessName.Text;
            organization.SicCode = TxtSicCode.Text;
            organization.FullDescription = TxtFullDescription.Text;
            organization.PhoneNumber = TxtPhoneNumber.Text;
            organization.Fax = TxtFax.Text;
            organization.Email = TxtEmail.Text;
            organization.WebAddress = TxtWebAddress.Text;
            organization.CharityNumber = TxtCharityNumber.Text;
            organization.CompanyNumber = TxtCompanyNumber.Text;
            organization.PreferredOrganization = CbxPreferredOrganization.Checked;
            string organizationSpecialism = string.Empty;
            string serviceDisablities = string.Empty;
            string serviceBarriers = string.Empty;
            string serviceBenefits = string.Empty;
            string serviceCircumstancesS = string.Empty;
            string serviceEnthnicity = string.Empty;
            string accreditation = string.Empty;

            foreach (ListItem item in CblOrgSpecial.Items)
            {
                if (item.Selected)
                {
                    organizationSpecialism += item.Value + ";";
                }
            }
            foreach (ListItem item in CblDisabilities.Items)
            {
                if (item.Selected)
                {
                    serviceDisablities += item.Value + ";";
                }
            }
            foreach (ListItem item in CblBarriers.Items)
            {
                if (item.Selected)
                {
                    serviceBarriers += item.Value + ";";
                }
            }
            foreach (ListItem item in CblBenefits.Items)
            {
                if (item.Selected)
                {
                    serviceBenefits += item.Value + ";";
                }
            }
            foreach (ListItem item in CblCircumstances.Items)
            {
                if (item.Selected)
                {
                    serviceCircumstancesS += item.Value + ";";
                }
            }
            foreach (ListItem item in CblEthnicity.Items)
            {
                if (item.Selected)
                {
                    serviceEnthnicity += item.Value + ";";
                }
            }

            foreach (ListItem item in CblAccreditation.Items)
            {
                if (item.Selected)
                {
                    accreditation += item.Value + ";";
                }
            }
            organization.ServiceDisabilities = serviceDisablities;
            organization.OrganizationSpecialism = organizationSpecialism;

            organization.ServiceBarriers = serviceBarriers;
            organization.ServiceBenefits = serviceBenefits;
            organization.ServiceCircumstances = serviceCircumstancesS;
            organization.ServiceEthnicity = serviceEnthnicity;
            organization.Accreditation = accreditation;
            var programmeIds = new List<int>();
            var serviceIds = new List<int>();
            var i = 0;
            foreach (ListItem item in CblPrograms.Items)
            {
                
                if (item.Selected)
                {
                   
                    programmeIds.Add(Convert.ToInt32(item.Value));
                  
                }
            }
            var j = 0;
            foreach (ListItem item in CblServices.Items)
            {
                if (item.Selected)
                {
                 
                    serviceIds.Add(Convert.ToInt32(item.Value));
                  
                }
            }

            bool result = bus.AddOrganization(organization, programmeIds, serviceIds);
            if (result)
            {
                PanelSuccess.Visible = true;
                Success("Save Organization successfully.");
                ClearValue();
            }
            else
            {
                PanelSuccess.Visible = false;
            }
        }

      

        private void ClearValue()
        {
            TxtOrganizationName.Text = string.Empty;
            TxtShortDescription.Text = string.Empty;

            txtContact.Text = string.Empty;
            TxtAddressLine1.Text = string.Empty;
            TxtAddressLine2.Text = string.Empty;
            TxtAddressLine3.Text = string.Empty;

            txtPostCode.Text = string.Empty;
            TxtCityTown.Text = string.Empty;
            TxtCounty.Text = string.Empty;
            txtBussinessName.Text = string.Empty;
            TxtSicCode.Text = string.Empty;
            TxtFullDescription.Text = string.Empty;
            TxtPhoneNumber.Text = string.Empty;
            TxtFax.Text = string.Empty;
            TxtEmail.Text = string.Empty;
            TxtWebAddress.Text = string.Empty;
            TxtCharityNumber.Text = string.Empty;
            TxtCompanyNumber.Text = string.Empty;
            CbxPreferredOrganization.Checked = false;
            CbxInterest.Checked = false;
            lblCheckOrgName.Text = string.Empty;
            if (Session.Contents["contact"] != null)
            {
                Session["contact"] = null;
            }
            if (Session.Contents["Address"] != null)
            {
                Session["Address"] = null;
            }
            if (Session.Contents["Bussname"] != null)
            {
                Session["Bussname"] = null;
            }
            if (Session.Contents["Sic"] != null)
            {
                Session["Sic"] = null;
            }
            foreach (ListItem item in CblPrograms.Items)
            {
                item.Selected = false;
            }
            foreach (ListItem item in CblServices.Items)
            {
                item.Selected = false;
            }

            foreach (ListItem item in CblOrgSpecial.Items)
            {
                item.Selected = false;
            }
            foreach (ListItem item in CblDisabilities.Items)
            {
                item.Selected = false;
            }
            foreach (ListItem item in CblBarriers.Items)
            {
                item.Selected = false;
            }
            foreach (ListItem item in CblBenefits.Items)
            {
                item.Selected = false;
            }
            foreach (ListItem item in CblCircumstances.Items)
            {
                item.Selected = false;
            }
            foreach (ListItem item in CblEthnicity.Items)
            {
                item.Selected = false;
            }

            foreach (ListItem item in CblAccreditation.Items)
            {
                item.Selected = false;
            }
        }

        protected void CbxInterest_CheckedChanged(object sender, EventArgs e)
        {
            LinkDetails03.Visible = CbxInterest.Checked;
            DetailsPanel03.Visible = CbxInterest.Checked;
            GetService();
            GetProgramme();

        }

        protected void BtnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("../Views/ListOrganizations.aspx");
        }

    }
}